package com.estacionamientos

class AbstractUsuario {

	String username
	String password
	String mail
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired
	List operacionesVigentes = new ArrayList()

	static constraints = {
		username blank: false, unique: true
		password blank: false
		enabled(nullable:false)
		operacionesVigentes(nullable:true)
		accountExpired(nullable:true)
		accountLocked(nullable:true)
		passwordExpired(nullable:true)
		mail(nullable:true)
	}

	static hasMany = [operacionesVigentes:OperacionVigente]
	
	static mapping = {
		cache:'nonstrict-read-write'
		operacionesVigentes cascade: 'all-delete-orphan'
		operacionesVigentes fetch:'select'

		password column: '`password`'
		tablePerHierarchy false
	}

	Set<Rol> getAuthorities() {
		AbstractUsuarioRol.findAllByAbstractUsuario(this).collect { it.rol } as Set
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractUsuario other = (AbstractUsuario) obj;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
	
}
